package com.hopu.oa.controller.notice;

import com.hopu.oa.entity.notice.AoaNoticeList;
import com.hopu.oa.entity.user.AoaUser;
import com.hopu.oa.service.impl.notice.AoaNoticeListServiceImpl;
import com.hopu.oa.service.impl.notice.NoticeManageServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
public class inFormListController {

    @Autowired
    private AoaNoticeListServiceImpl aoaNoticeListService;

    @Autowired
    private NoticeManageServiceImp noticeListService;

    @RequestMapping("/infromlist")
    public String informlist(HttpSession session, Model model, String find, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "3") int size) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        System.out.println("find = " + find);
        int totalElements = aoaNoticeListService.queryNoticeListById(user.getUserId(), find, 0, 0).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaNoticeList> aoaNoticeLists = aoaNoticeListService.queryNoticeListById(user.getUserId(), find, (page - 1) * size, size);
        for (AoaNoticeList aoaNoticeList : aoaNoticeLists) {
            System.out.println(aoaNoticeList);
        }
        HashMap<String, Integer> map1 = new HashMap<>();
        map1.put("totalElements", totalElements);
        map1.put("totalPages", totalPages);
        map1.put("number", page);
        map1.put("size", size);

        model.addAttribute("page", map1);
        model.addAttribute("list", aoaNoticeLists);
        model.addAttribute("find", find);
        model.addAttribute("url", "informlistpaging");

        return "inform/informlist";
    }

    @RequestMapping("informlistpaging")
    public String informlistpaging(HttpSession session, Model model, String find, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "3") int size) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        System.out.println("find = " + find);
        int totalElements = aoaNoticeListService.queryNoticeListById(user.getUserId(), find, 0, 0).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaNoticeList> aoaNoticeLists = aoaNoticeListService.queryNoticeListById(user.getUserId(), find, (page - 1) * size, size);
        for (AoaNoticeList aoaNoticeList : aoaNoticeLists) {
            System.out.println(aoaNoticeList);
        }
        HashMap<String, Integer> map1 = new HashMap<>();
        map1.put("totalElements", totalElements);
        map1.put("totalPages", totalPages);
        map1.put("number", page);
        map1.put("size", size);

        model.addAttribute("page", map1);
        model.addAttribute("list", aoaNoticeLists);
        model.addAttribute("find", find);
        model.addAttribute("url", "informlistpaging");

        return "inform/informlistpaging";
    }

    @RequestMapping("forwardother")
    public String forwardother(Model model, Integer noticeId, Integer page, HttpSession session) {
        System.out.println("noticeId = " + noticeId);

        AoaUser aoaUser = (AoaUser) session.getAttribute("user");


        boolean b = noticeListService.forwardInform(noticeListService.queryNoticeList(noticeId), aoaUser);

        return "redirect:infromlist?page=" + page;
    }
}
